/**
 * @author thuang513
 */



function Map(){

	this.data = null;
	this.geomap = null;
	this.hidemap = null;
	this.qfacts = null;
	this.options = {};
	this.stateAbbr = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'];
}

Map.prototype.init = function(){
	
	try {
	
		this.data = new google.visualization.DataTable();
		
		this.data.addRows(50);
		
		this.data.addColumn('string', 'State');
		this.data.addColumn('number', 'Value');
		
		this.data.setValue(0, 0, 'Alabama');
		this.data.setValue(1, 0, 'Alaska');
		this.data.setValue(2, 0, 'Arizona');
		this.data.setValue(3, 0, 'Arkansas');
		this.data.setValue(4, 0, 'California');
		this.data.setValue(5, 0, 'Colorado');
		this.data.setValue(6, 0, 'Connecticut');
		this.data.setValue(7, 0, 'Delaware');
		this.data.setValue(8, 0, 'Florida');
		this.data.setValue(9, 0, 'Georgia');
		this.data.setValue(10, 0, 'Hawaii');
		this.data.setValue(11, 0, 'Idaho');
		this.data.setValue(12, 0, 'Illinois');
		this.data.setValue(13, 0, 'Indiana');
		this.data.setValue(14, 0, 'Iowa');
		this.data.setValue(15, 0, 'Kansas');
		this.data.setValue(16, 0, 'Kentucky');
		this.data.setValue(17, 0, 'Louisiana');
		this.data.setValue(18, 0, 'Maine');
		this.data.setValue(19, 0, 'Maryland');
		this.data.setValue(20, 0, 'Massachusetts');
		this.data.setValue(21, 0, 'Michigan');
		this.data.setValue(22, 0, 'Minnesota');
		this.data.setValue(23, 0, 'Mississippi');
		this.data.setValue(24, 0, 'Missouri');
		this.data.setValue(25, 0, 'Montana');
		this.data.setValue(26, 0, 'Nebraska');
		this.data.setValue(27, 0, 'Nevada');
		this.data.setValue(28, 0, 'New Hampshire');
		this.data.setValue(29, 0, 'New Jersey');
		this.data.setValue(30, 0, 'New Mexico');
		this.data.setValue(31, 0, 'New York');
		this.data.setValue(32, 0, 'North Carolina');
		this.data.setValue(33, 0, 'North Dakota');
		this.data.setValue(34, 0, 'Ohio');
		this.data.setValue(35, 0, 'Oklahoma');
		this.data.setValue(36, 0, 'Oregon');
		this.data.setValue(37, 0, 'Pennsylvania');
		this.data.setValue(38, 0, 'Rhode Island');
		this.data.setValue(39, 0, 'South Carolina');
		this.data.setValue(40, 0, 'South Dakota');
		this.data.setValue(41, 0, 'Tennessee');
		this.data.setValue(42, 0, 'Texas');
		this.data.setValue(43, 0, 'Utah');
		this.data.setValue(44, 0, 'Vermont');
		this.data.setValue(45, 0, 'Virginia');
		this.data.setValue(46, 0, 'Washington');
		this.data.setValue(47, 0, 'West Virginia');
		this.data.setValue(48, 0, 'Wisconsin');
		this.data.setValue(49, 0, 'Wyoming');
		
		for (var i = 0; i < 50; i++) {
		
			this.data.setValue(i, 1, 0);
			
		}
		
		this.options['region'] = 'US';
		this.options['width'] = 800;
		this.options['height'] = 500;
		this.options['showLegend'] = false;
		this.options['color'] = [0xE0FFD4, 0xA5EF63, 0x50AA00, 0x267114]
		
		this.geomap = new google.visualization.GeoMap($('#visualization')[0]);
		this.hidemap = new google.visualization.GeoMap($('#hide')[0]);
		this.qfacts = new google.visualization.GeoMap($('#qfacts')[0]);
		
		this.geomap.draw(this.data, this.options);
		this.hidemap.draw(this.data, this.options);
		this.qfacts.draw(this.data, this.options);
		
		var thisobject = this;
		
		google.visualization.events.addListener(this.geomap, 'select', function(){
			thisobject.selectHandler();
		});
		google.visualization.events.addListener(this.hidemap, 'select', function(){
			thisobject.selectHandler();
		});
		google.visualization.events.addListener(this.qfacts, 'select', function(){
			thisobject.qFactsHandler();
		});
		
	}
	 catch (e) {
        alert("Error in init() " + e);
    }
	
}
	

Map.prototype.selectHandler = function(){

    try {
    

        var geo = $('#visualization');
        var selection = null;
		
        if (geo.is(':visible')) {
            selection = this.geomap.getSelection();
        }
        else {
            selection = this.hidemap.getSelection();
        }
        
        if (this.data.getValue(selection[0].row, 1) == 1) 
            this.data.setValue(selection[0].row, 1, 0);
        else 
            this.data.setValue(selection[0].row, 1, 1);
       

//        other.draw(this.data, this.options);
        
     //   this.updateList();
        
      //  setTimeout('', 700);
		this.hide();

    } 
    catch (e) {
        alert("Error in selectHandler() " + e);
    }
    
}

Map.prototype.hide = function(){
    try {

        var geo = $('#visualization');
        var hide = $('#hide');
        var map = null;
		var other = null;
		
        if (geo.is(':visible')) {
			hide.show();
			this.hidemap.draw(this.data, this.options);
            geo.hide();
        }
        else {
			geo.show();
            this.geomap.draw(this.data, this.options);
			hide.hide();
        }
    } 
    
    catch (e) {
        alert("Error in hide() " + e);
    }
    
}

Map.prototype.updateList = function(){

    try {
    
		
        
        var obj = $("#list");
        
        var listOfName = "<div class='states'>States:</div> ";
        
        for (var i = 0; i < 50; i++) {
        
            if (this.data.getValue(i, 1) == 1) {
            
                listOfName = listOfName + " " + this.data.getValue(i, 0) + ".";

            }
        }
        
        obj.html(listOfName);
		
		
		updateMapSelection();
    } 
    catch (e) {
        alert("Error in updateList() " + e);
    }
    
}

Map.prototype.clearStates = function(){

  
    
    for (var i = 0; i < 50; i++) {
    
        this.data.setValue(i, 1, 0);
        
    }
    
        
    this.updateList();
    
   this.hide();
}

Map.prototype.qFactsShow = function() {
	
	try {
		
		$('#qFactsToggle').attr({onClick: "javascript:theMap.qFactsHide()", id: "qFactsToggle",});
		$('#qFactsToggle').css({
			"width": "75px",
		});
		$('#qFactsToggle').html("Selection Map");
		
	//	$('#next').hide();
		$('#visualization').hide();
		$('#hide').hide();
		$('#list').hide();
		$('#qfacts').show();
	}
	 catch (e) {
        alert("Error in qFactsShow() " + e);
    }
}

Map.prototype.qFactsHide = function() {
		
	
	this.geomap.draw(this.data,this.options);
	$('#visualization').show();
	
	//$('#next').show();
	
	$('#qfacts').hide();
	$('#list').show();
	
	$('#qFactsToggle').attr({onClick: "javascript:theMap.qFactsShow()", id: "qFactsToggle"});
	$('#qFactsToggle').css({"width" : "60px"});
	$('#qFactsToggle').html("Quick Facts");
}

Map.prototype.qFactsHandler = function() {
	
	try {
		
		var mss = [4142,558,8724,3259,60065,7246,4802,1196,30233,13952,1418,1315,16995,8413,3394,2802,4582,4373,1982,7284,10836,11889,7733,2193,8222,955,1953,3723,1747,11281,2914,28183,10781,572,14137,4219,5651,14773,1369,5192,542,7730,27061,2978,1004,10148,10536,1667,6965,771]
		var fss = [4555, 759, 7897, 2472, 46456, 7472, 4820, 1217, 23032, 10765, 1427, 1173, 13038, 6999, 3031, 3432, 4639, 4484, 2521, 8009, 12522, 11175, 6910, 1688, 6432, 995, 1543, 3018, 2596, 9520, 3129, 24096, 9629, 395, 13898, 3981, 6830, 14708, 1620, 5211, 581, 6383, 22435, 2187, 1205, 9086, 10844, 1961, 6864, 484]
		var allss = [8697, 1317, 16621, 5731, 106521, 14718, 9622, 2413, 53265, 24717, 2845, 2488, 30033, 15412, 6425, 6234, 9221, 8857, 4503, 15293, 23358, 23064, 14643, 3881, 14654, 1950, 3496, 6741, 4343, 20801, 6043, 52279, 20410, 967, 28035, 8200, 12481, 29481, 2989, 10403, 1123, 14113, 49496, 5165, 2209, 19234, 21380, 3628, 13829, 1255]
		
		var i = this.qfacts.getSelection()[0].row;
       
		$('#dialog').html('<b>Male Same Sex Couples:</b> ');
		$('#dialog').append(mss[i]);
		$('#dialog').append('<br><b>Female Same Sex Couples:</b> ');
		$('#dialog').append(fss[i]);
		$('#dialog').append('<br><b>All Same Sex Couples:</b> ');
		$('#dialog').append(allss[i]);
		
		$('#dialog').dialog( "option", "title", this.data.getValue(this.qfacts.getSelection()[0].row, 0));
		$('#dialog').dialog('open');
	} 
	catch (e) {
		// alert("Error in qFactsHandler" + e);
	}


}